<template>
	<view class="entrance">
		<view>
			<view class="entrance_jurisdiction" style="display: flex;" >
				<view>门禁权限</view>
				<switch :checked="is_open" disabled style="margin-left: auto;" color="#4CD964" />
			</view>

			<view class="entrance_feature">
				<view style="font-size: 12px;">功能小提示:</view>
				<view class="min_case">
					打开门禁权限按钮后，业主主账号及子账号可以自动开启所在小区门禁，为开启或账户到期，物业费用拖欠将自动关闭
				</view>
			</view>
			<view style="border: 4px solid #F1F1F1;"></view>
			<view class="entrance_jurisdiction">
				<h4>子账户管理</h4>
				<view class="min_case" style="margin-left: 2px;">下列账户均匀主账号授权开通的子账号，为保障您和其他用户的安全，请谨慎开通</view>
			</view>


			<view class="baco_name">
				<view class="baco_guard">
					<view style="display: flex;">
						<view style="flex: 1;">姓名</view>
						<view style="flex: 1;">账号</view>
						<view style="flex: 1;">操作</view>
					</view>
					<view v-for="(item,index) in data" style="display: flex;margin-top: 10px;">
						<view style="flex: 1;">{{item.username}}</view>
						<view style="flex: 1;">{{item.mobile_number}}</view>
						<view v-if="item.status == 1" style="flex: 1;"><button v-if="is_host" size="mini" type="default"
								@click="updateAuth(item, 3)">解除授权</button></view>
						<view v-if="item.status == 3" style="flex: 1;"><button v-if="is_host" size="mini" type="default"
								@click="updateAuth(item, 1)">授权</button></view>
					</view>
				</view>

				<button v-if="is_host" type="primary"  style="margin: 30px 0; padding: 5px 0; font-size: 12px;" @click="create()">
					增加子账户
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: [],
				is_open: false,
				is_host: false,
			}
		},
		onLoad() {

		},
		onShow() {
			this.getData();
			this.init();
		},
		methods: {
			init() {
				let status = uni.getStorageSync('status', 0);
				let is_host = uni.getStorageSync('muid', 0);
				if (status == 1) {
					this.is_open = true;
				}
				if (is_host == 0) {
					this.is_host = true;
				}
			},
			getData() {
				let that = this;
				let uid = uni.getStorageSync('uid');
				
				this.$api.getSubUser({
					uid: uid
				}).then(res => {
					that.data = res.data;
				})
			},
			create() {
				uni.navigateTo({
					url: '/pages/user/subcreate'
				})
			},
			updateAuth(item, status) {
				let that = this;
				uni.showModal({
					title: '是否操作',
					success: function(res) {
						if (res.confirm) {
							that.$api.postUpdateAuth({
								id: item.id,
								status: status
							}).then(res => {
								if (res.meta.code == '000') {
									item.status = status;
								}
							})
						} else if (res.cancel) {}
					}
				});
			}
		}
	}
</script>

<style>
	.entrance {
		font-size: 15px;
	}

	.entrance_jurisdiction {
		margin: 15px 18px;

	}

	.entrance_feature {
		margin: 15px 18px;
		border-radius: 5px;
		padding: 15px;
		border: 1px solid #C8C7CC;
	}

	.min_case {
		color: #999999;
		padding-top: 8px;
		line-height: 1.6;
		font-size: 12px;
	}

	.baco_name {
		margin: 20px 18px;
		border-radius: 5px;
		padding: 15px;
		border: 1px solid #C8C7CC;

	}

	.baco_guard {
		padding-left: 5px;
	}

	.minm_case {
		color: #999999;
		padding-top: 8px;
	}

	.button.guard {
		background: #007AFF;
		text-align: center;
		margin-top: 20px;
		padding: 10px;
		border-radius: 5px;
		font-size: 12px;
		color: #FFFFFF;
	}
</style>
